package com.ahaoyou.component;

import com.ahaoyou.entity.SysJob;
import com.ahaoyou.service.SysJobService;
import com.ahaoyou.utils.ScheduleUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Scheduler;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author LH
 * @create 2022/3/9 19:56
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class InitializationJobs implements CommandLineRunner {

    private final SysJobService sysJobService;

    private final Scheduler scheduler;

    @Override
    public void run(String... args) throws Exception {
        List<SysJob> sysJobs = sysJobService.selectAll();
        for (SysJob sysJob : sysJobs) {
            ScheduleUtils.createScheduleJob(scheduler, sysJob);
            log.info("初始化任务<{}>成功", sysJobs);
        }
    }
}